﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
using WX.WMS.Common;

namespace WX.WMS.BLL
{
    public class InStockItemItemBLL : BaseBLL
    {
        private BaseDAO DAO = null;
        public InStockItemItemBLL()
        {
            DAO = new BaseDAO();
        }

        public bool IsExisted(string InStockID, string ProductDetailID)
        {
            if (DAO.T_INSTOCK_ITEMs.Where(t => t.INSTOCK_ID.ToString() == InStockID 
                                                && t.PRODUCT_DETAIL_ID.ToString() == ProductDetailID).Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public IEnumerable<T_INSTOCK_ITEM> GetInStockItemList()
        {
            var list = DAO.T_INSTOCK_ITEMs.Where(WhereCondition);

            if (OrderCondition != "")
            {
                list = list.OrderBy(OrderCondition);
            }

            return list;
        }

        public T_INSTOCK_ITEM GetInStockItemByID(string ID)
        {
            return DAO.T_INSTOCK_ITEMs.Where(t => t.ID.ToString() == ID).SingleOrDefault();
        }

        public int AddInStockItem(T_INSTOCK_ITEM InStockItem)
        {
            try
            {
                InStockItem.ID = DAO.GetGUID();

                DAO.T_INSTOCK_ITEMs.InsertOnSubmit(InStockItem);
                DAO.SubmitChanges();

                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AddInStockItem(T_INSTOCK_ITEM InStockItem, out string InStockItemGuid)
        {
            try
            {
                InStockItem.ID = DAO.GetGUID();
                
                DAO.T_INSTOCK_ITEMs.InsertOnSubmit(InStockItem);
                DAO.SubmitChanges();

                InStockItemGuid = InStockItem.ID.ToString();
                return 1;
            }
            catch (Exception ex)
            {
                InStockItemGuid = "";
                return -1;
            }
        }

        public int UpdateInStockItem(T_INSTOCK_ITEM InStockItem)
        {
            try
            {
                T_INSTOCK_ITEM updateEntity = DAO.T_INSTOCK_ITEMs.SingleOrDefault(t=>t.ID.ToString()==InStockItem.ID.ToString());
                if (updateEntity == null) 
                    return -1;

                updateEntity.BAR_CODE = InStockItem.BAR_CODE;
                updateEntity.FLOOR_ID = InStockItem.FLOOR_ID;
                updateEntity.GROSS_WEIGHT = InStockItem.GROSS_WEIGHT;
                updateEntity.INSTOCK_ID = InStockItem.INSTOCK_ID;
                updateEntity.NET_WEIGHT = InStockItem.NET_WEIGHT;
                updateEntity.PACKET_QTY = InStockItem.PACKET_QTY;
                updateEntity.PACKET_QTY2 = InStockItem.PACKET_QTY2;
                updateEntity.PRODUCT_DETAIL_ID = InStockItem.PRODUCT_DETAIL_ID;
                updateEntity.REMARK = InStockItem.REMARK;

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteInStockItem(string ID)
        {
            try
            {
                T_INSTOCK_ITEM deleteEntity = DAO.T_INSTOCK_ITEMs.SingleOrDefault(t => t.ID.ToString() == ID);
                if (deleteEntity == null) 
                    return -1;
                DAO.T_INSTOCK_ITEMs.DeleteOnSubmit(deleteEntity);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteInStockItems(List<string> IDs)
        {
            try
            {
                foreach (string ID in IDs)
                {
                    T_INSTOCK_ITEM deleteEntity = DAO.T_INSTOCK_ITEMs.SingleOrDefault(t => t.ID.ToString() == ID);
                    if (deleteEntity == null)
                        return -1;
                    DAO.T_INSTOCK_ITEMs.DeleteOnSubmit(deleteEntity);
                }

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    }
}
